using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;

public class InputManager : MonoBehaviour
{
    private string get;
    void Awake()
    {

    }


    void ChangeText(int index)
    {
        if (index < 7)
        {
            SettingInit.text[0].text = Settings.setting.opa[3] + "/" + Settings.setting.opa[4] + "/" + Settings.setting.opa[5] + "/" + Settings.setting.opa[6];
        }
        else if (index == 7)
        {
            SettingInit.text[1].text = Settings.setting.opa[7];
        }
        else if (index == 8)
        {
            SettingInit.text[2].text = Settings.setting.opa[8];
        }
        else if (index == 9)
        {
            SettingInit.text[3].text = Settings.setting.opa[9];
        }
    }
    void BindOpa(InputAction NeedChange,bool isGroup, int setIndex)
    {
        NeedChange.Disable();
        if (!isGroup)
        {
            NeedChange.PerformInteractiveRebinding().WithControlsExcluding("Mouse").Start().OnComplete(
                operation =>
                {
                    string[] find = operation.selectedControl.ToString().Split('/');
                    get = find[find.Length - 1];
                    Settings.setting.opa[setIndex] = get;
                    ChangeText(setIndex);
                    operation.Dispose(); // �����ͷ���Դ
                }
                );

            return;
        }
        else
        {
            NeedChange.PerformInteractiveRebinding(setIndex-2).WithControlsExcluding("Mouse").Start().OnComplete(
                operation =>
                {
                    string[] find = operation.selectedControl.ToString().Split('/');
                    get = find[find.Length - 1];
                    Settings.setting.opa[setIndex] = get;
                    ChangeText(setIndex);
                    if (setIndex + 1 < 7) BindOpa(NeedChange,isGroup, setIndex + 1);
                    operation.Dispose();
                }
                );
        }
        NeedChange.Enable();
    }
    void ChangeBind(InputAction NeedChange, int index, int setIndex, bool isGroup)
    {
        if (!isGroup)
        {

            BindOpa(NeedChange,isGroup, setIndex);
        }
        else
        {
            BindOpa(NeedChange,isGroup, setIndex);
        }
        Settings.inputActions.PlayerInputAction.Enable();//�˺�����֮������ý�����ļ�λ��Ҫ����
    }
    public void ChangeJump()
    {
        ChangeBind(Settings.inputActions.PlayerInputAction.JumpBinding, 7, 7, false);
        Debug.Log(Settings.inputActions.PlayerInputAction.JumpBinding);
    }

    public void ChangeInteraction()
    {
        ChangeBind(Settings.inputActions.PlayerInputAction.InteractionBinding, 8, 8, false);
    }

    public void ChangeBackPack()
    {
        ChangeBind(Settings.inputActions.PlayerInputAction.TaskBinding, 9, 9, false);
    }

    public void ChangeMove()
    {
        ChangeBind(Settings.inputActions.PlayerInputAction.MoveBinding, 3,3, true);
    }

}
